package com.szholly.utils.spring.springmvc.route;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UrlPathHelper;

/**
 * 定义功能注册下的功能路由器
 */
public class RouteHandler implements HandlerAdapter {

	private UrlPathHelper urlHelper;

	public RouteHandler() {
		urlHelper = new UrlPathHelper();
	}
	
	@Override
	public long getLastModified(HttpServletRequest request, Object formHandler) {
		return -1;
	}

	@Override
	public ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object formHandler) throws Exception {
		// ��ȡURL
		String url = urlHelper.getLookupPathForRequest(request);
		url = url.replace(".do", "");
		if (url.startsWith("/")) {
			url = url.substring(1);
		}
		if (RouteController.Routes.containsKey(url)) {
			String routesUrl = RouteController.Routes.get(url).getFunctionUrl();
			ModelAndView view = new ModelAndView();
			view.setViewName(routesUrl);
			return view;
		}
		return null;
	}

	@Override
	public boolean supports(Object obj) {
		if (obj instanceof RouteHandler) {
			return true;
		} else {
			return false;
		}
	}
}